<?php
class AdminService extends BaseDbService
{
	public function __construct()
	{
		$this->dao	= new BaseDao('admin');
	}
	
	public function get($id)
	{
		$info	= parent::get($id);
		if(!$info)
		{
			return false;
		}
		
		if(!empty($info['login_ip']))
		{
			$info['login_ip']	= Util::number2ip($info['login_ip']);
		}
		if(!empty($info['last_login_ip']))
		{
			$info['last_login_ip']	= Util::number2ip($info['last_login_ip']);
		}
		
		return $info;
	}
	
	public function getByName($username)
	{
		return parent::fetch(array('username'=>'eq:'.$username));
	}
	
	public function checkUserLogin($username, $password)
	{
		$user	= $this->getByName($username);
		if(!$user)
		{
			return false;
		}
		
		if(Util::generatePassword($password,$user['secques'])!=$user['password'])
		{
			return false;
		}
		
		return $user;
	}
	
	public function updateLoginInfo($id,$ip,$loginTime)
	{
		$info	= parent::get($id);
		if(!$info)
		{
			return false;
		}
		
		$data['last_login_ip']		= $info['login_ip'];
		if(!empty($info['login_time']))
		{
			$data['last_login_time']	= $info['login_time'];
		}
		$data['login_times']		= $info['login_times']+1;
		$data['login_time']			= $loginTime;
		$data['login_ip']			= Util::ip2number($ip);
		
		return parent::update($id,$data);
	}
}